package example.documentbased.editorModule.controller
{
	import example.documentbased.editorModule.models.EditorModuleModel;
	import example.documentbased.mainModule.models.factories.ItemsFactory;
	import example.documentbased.mainModule.models.vo.DocumentVO;
	import example.documentbased.mainModule.models.vo.EditorVO;
	import example.documentbased.mainModule.utils.ItemUtils;
	
	import mx.utils.UIDUtil;
	
	import org.etherframework.controller.ReactiveCommand;
	
	import raix.reactive.IObservable;
	
	public class CreateDocumentCommand extends ReactiveCommand
	{
		private var _editorModel:EditorModuleModel;
		private var _itemsFactory:ItemsFactory;
		private var _editorController:IEditorController;
		
		public function CreateDocumentCommand(editorModel:EditorModuleModel, 
											  itemsFactory:ItemsFactory,
											  editorController:IEditorController)
		{
			_editorModel = editorModel;
			_itemsFactory = itemsFactory;
			_editorController = editorController;
			markAsInitialized();
		}
		
		override public function observable():IObservable
		{
			var name:String = ItemUtils.createUniqueName(_editorModel.openedDocuments, "New");
			var editorVO:EditorVO = _editorModel.editor;
			var document:DocumentVO = _itemsFactory.createDocumentVO("", UIDUtil.createUID(), 
				editorVO.id, name, "");
			
			return _editorController.getOpenDocumentCommand(document).observable();
		}
	}
}